package com.dolphin.common.component.var.model;

import com.google.common.collect.Lists;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author shida
 */
@Getter
@Setter
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Slf4j
public abstract class ExtVarModel extends BaseVarModel{

    private String[] depVars;
    private List<BaseVarModel> depVarModels;
    private List<String> depFailBids = new ArrayList<>();

    public ExtVarModel() {
    }

    public void addDepFailBid(String bid) {
        depFailBids.add(bid);
    }

    public void buildDependencies(Map<String, BaseVarModel> varBidMap){
        if(varBidMap == null || varBidMap.isEmpty()){
            return;
        }

        List<BaseVarModel> depModels = Lists.newArrayList();
        for (String depVar : depVars){
            BaseVarModel depModel = varBidMap.get(depVar);
            if (depModel != null){
                depModels.add(depModel);
            }else {
                log.warn("{} is not found in varBidMap:{}", depVar, varBidMap);
                this.addDepFailBid(depVar);
            }
        }
        this.setDepVarModels(depModels);
    }

}
